﻿using System.Collections;
using UnityEngine;

public class BehaviourUtil
{
    private static MonoBehaviour globalCoroutine;

    public static Coroutine StartCoroutine(IEnumerator routine)
    {
        if (routine == null)
        {
            return null;
        }
        return GlobalCoroutine.StartCoroutine(routine);
    }

    public static void StopCoroutine(IEnumerator routine)
    {
        GlobalCoroutine.StopCoroutine(routine);
    }

    public static void StopCoroutine(string methodName)
    {
        GlobalCoroutine.StopCoroutine(methodName);
    }

    public static void StopAllCoroutines()
    {
        EDebug.Log("StopAllCoroutines");
        GlobalCoroutine.StopAllCoroutines();
    }

    public static MonoBehaviour GlobalCoroutine
    {
        get
        {
            if (globalCoroutine == null)
            {
                GameObject target = new GameObject("globalCoroutine");
                globalCoroutine = target.AddComponent<MonoBehaviour>();
                Object.DontDestroyOnLoad(target);
            }
            return globalCoroutine;
        }
    }
}